package com.xtoad.ecms.baseinfo.converter;

import com.xtoad.ecms.baseinfo.dto.RoleDTO;
import com.xtoad.ecms.baseinfo.model.Role;
import com.xtoad.ecms.common.web.base.IConverter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色转换器实现类
 *
 * @author xtoad
 * @date 2020/05/29
 */
@Component
public class RoleConverter implements IConverter<RoleDTO, Role> {

    /**
     * Model To DTO
     *
     * @param model 源
     * @return 目标
     */
    @Override
    public RoleDTO toDto(Role model) {
        RoleDTO dto = new RoleDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setCreateTime(model.getCreateTime());
        dto.setCreateUser(model.getCreateUser());
        dto.setLastUpdateTime(model.getLastUpdateTime());
        dto.setLastUpdateUser(model.getLastUpdateUser());

        return dto;
    }

    /**
     * DTO To Model
     *
     * @param dto 源
     * @return 目标
     */
    @Override
    public Role toModel(RoleDTO dto) {
        Role model = new Role();
        model.setId(dto.getId());
        model.setName(dto.getName());
        model.setCode(dto.getCode());
        model.setCreateTime(dto.getCreateTime());
        model.setCreateUser(dto.getCreateUser());
        model.setLastUpdateTime(dto.getLastUpdateTime());
        model.setLastUpdateUser(dto.getLastUpdateUser());

        return model;
    }

    /**
     * 批量Model To DTO
     *
     * @param models 源
     * @return 目标
     */
    @Override
    public List<RoleDTO> toDtoList(List<Role> models) {
        List<RoleDTO> dtoList = new ArrayList<>();
        models.forEach(model -> dtoList.add(this.toDto(model)));
        return dtoList;
    }

    /**
     * 批量DTO To Model
     *
     * @param dtos 源
     * @return 目标
     */
    @Override
    public List<Role> toModelList(List<RoleDTO> dtos) {
        List<Role> modelList = new ArrayList<>();
        dtos.forEach(dto -> modelList.add(this.toModel(dto)));
        return modelList;
    }
}
